﻿// Author: Thiago Boufleuhr
// E-mail: boufleuhr@gmail.com

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Globalization;
using System.Xml.Serialization;
using BoufleuhrSolutions.DynamicDTO;

namespace BoufleuhrSolutions.DynamicDTOTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Person p = new Person();
            p.Id = 10;
            p.Name = "Thiago";
            p.LastName = "Boufleuhr";
            p.Address = new Address()
            {
                StreetName = "Benjamin Constant",
                City = new City()
                {
                    Id = 987,
                    Name = "Blumenau",
                },
            };

            var pessoaDto = DataTransferHelper.Generate(p);

            var serialized = Serialize(pessoaDto);
            var deserialized = Deserialize<DataTransferObject>(serialized);

            var pessoaDtoRebuilded = DataTransferHelper.Rebuild(deserialized);

            Console.Write(deserialized.DataTransferProperties.Count);
        }

        static string Serialize(object value)
        {
            var content = string.Empty;
            using (StringWriter writer = new StringWriter(CultureInfo.CurrentCulture))
            {
                XmlSerializer serializer = new XmlSerializer(value.GetType());
                serializer.Serialize(writer, value);
                content = writer.ToString();
            }

            return content;
        }

        static T Deserialize<T>(object value)
        {
            StringReader reader = new StringReader(value.ToString());
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            T builded = (T)serializer.Deserialize(reader);
            return builded;
        }
    }
}
